package com.controller;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.common.ajax.AjaxRes;
import com.common.mybatis.Page;
import com.common.utils.base.UuidUtil;
import com.entity.XinWenLanMu;
import com.entity.XinWenZiXun;
import com.entity.YongHu;
import com.service.XwlmService;
import com.service.XwzxService;

@Controller
@RequestMapping("/xwzx/manager")
public class XwzxController{
	@Autowired
	private XwzxService xwzxService;
	@Autowired
	private XwlmService xwlmService;


	
	@RequestMapping("/xwzx")
	public String xwzx(Model md,String id){
//		//ModelAndView mv = new ModelAndView();
//		Student student = new Student();
//		List<Student> studentList = studentService.find(student);
//		m.addAttribute("student",studentList.get(0));
		return "pages/xwzx";
	}
	@RequestMapping("/findByPage")
	@ResponseBody
	public AjaxRes findByPage(XinWenZiXun xwzx,Page<XinWenZiXun> page){
		AjaxRes res = new AjaxRes();
		page = xwzxService.findByPage(xwzx, page);
		res.setSucceed(page,"成功");
		return res;
	}
	@RequestMapping("/addxwzx")
	@ResponseBody
	public AjaxRes addxwzx(XinWenZiXun xwzx,String fujianId){
		AjaxRes res = new AjaxRes();
		String xwzxId=UuidUtil.get32UUID();
		XinWenLanMu xwlm=new XinWenLanMu();
		xwlm.setLmmc(xwzx.getLmmc());
		 xwlm=xwlmService.findByid(xwlm);
		System.out.println("_________________"+xwlm);
		xwzx.setXwlmbh(xwlm.getId());
		xwzx.setId(xwzxId);
		 Date dt = new Date(); 
		String dateStr = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(dt);
		xwzx.setFbsj(dateStr);
		xwzxService.insertFj(xwzx, fujianId);
		res.setResMsg("成功");
		return res;
	}
	@RequestMapping("/findTree")
	@ResponseBody
	public AjaxRes findTree(XinWenZiXun xwzx){
		AjaxRes res = new AjaxRes();
		List<XinWenZiXun> xwzxlist=xwzxService.findTree(xwzx);
		res.setSucceed(xwzxlist,"成功");
		return res;
	}
	@RequestMapping("/delxwzx")
	@ResponseBody
	public AjaxRes delxwzx(XinWenZiXun xwzx){
		AjaxRes res = new AjaxRes();
		xwzxService.delete(xwzx);
		res.setSucceedMsg("删除成功");
		return res;
	}
	
	@RequestMapping("/delAllxwzx")
	@ResponseBody
	public AjaxRes delAllxwzx(String cks){
		AjaxRes res = new AjaxRes(); 
		String[] ck = cks.split(",");
		List<XinWenZiXun> list = new ArrayList<XinWenZiXun>();
		for(String s:ck){
			XinWenZiXun xwzx = new XinWenZiXun();
			xwzx.setId(s);
			list.add(xwzx);
		}
		xwzxService.deleteBatch(list);
		res.setSucceedMsg("删除成功");
		return res;
	}

	
	@RequestMapping("/updatexwzx")
	@ResponseBody
	public AjaxRes updatexwzx(XinWenZiXun xwzx){
		AjaxRes res = new AjaxRes();
		XinWenLanMu xwlm=new XinWenLanMu();
		xwlm.setLmmc(xwzx.getLmmc());
		xwlm=xwlmService.findByid(xwlm);
		System.out.println("_________________"+xwlm);
		xwzx.setXwlmbh(xwlm.getId());
		xwzxService.update(xwzx);
		res.setSucceedMsg("修改成功");
		return res;
	}
	
	@RequestMapping("/find")
	@ResponseBody
	public AjaxRes find(XinWenZiXun xwzx){
		AjaxRes res = new AjaxRes();
		List<XinWenZiXun> xszxs= xwzxService.find(xwzx);
		res.setSucceed(xszxs,"获取数据成功");
		return res;
	}

}
